﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using System.Threading;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
/// <summary>
/// Will Remove Later
/// </summary>
public class RefrenceCheckerEditor
{

    public static RefrenceMapBuilder builder = new RefrenceMapBuilder();
    public static JobExcuteProgress jobExcuteProgress;
    private static RefrenceMap _Map;
    //[MenuItem("Window/RefrenceChecker/FindRefrence")]
    public static void FindSelectionObjRefrence()
    {
        builder = new RefrenceMapBuilder();
        builder.MainThreadClenaUp();

        var t = new Thread(BuildThread);
        t.Start(Application.dataPath);
        while (true)
        {
            string title = "正在创建资源引用查找表.....";

            if (jobExcuteProgress.totalCount == 0)
            {
                EditorUtility.DisplayProgressBar(title, "任务创建中...", 0);
                continue;
            }
            string workerInfo = string.Empty;
            if (jobExcuteProgress.workerJobList.Length != 0) 
            {
                workerInfo += "总工作线程数量: " + jobExcuteProgress.workerJobList.Length;
                for (int i = 0; i < jobExcuteProgress.workerJobList.Length; i++)
                {
                    workerInfo += "[工作线程: " + i + "] 执行任务数量 :" + jobExcuteProgress.workerJobList[i];
                }
                
            }
            
            string info = string.Format("总任务进度: {0} / {1} ", jobExcuteProgress.finishCount, jobExcuteProgress.totalCount);
            info += workerInfo;
            float percent = jobExcuteProgress.perCent;
            EditorUtility.DisplayProgressBar(title, info, percent);
            if (percent > 0.99f)
                break;
        }
        t.Join();
        _Map.SaveProjectRefrenceMap();
        _Map = null;
        EditorUtility.ClearProgressBar();
        EditorUtility.DisplayDialog("执行结束", "资源引用查找表构建结束！", "确定");

        ThreadPool.QueueUserWorkItem((i) => 
        {
            RefrenceMap.CleanCache();
            RefrenceMap.ReadFromProjectPathOrCache();
        });

    }
    //[MenuItem("Assets/查找引用该对象的资源")]
    public static void FindRefrence()
    {
        var obj = Selection.activeObject;
        if (obj != null)
        {
            RefrenceMap map = RefrenceMap.ReadFromProjectPathOrCache();
            if (map == null)
            {
                Debug.LogError("必须先构建资源查找表");
                return;
            }
            string path = AssetDatabase.GetAssetPath(Selection.activeObject);
            if (!string.IsNullOrEmpty(path))
            {
                var finded = map.QueryRefrence(path);
                if (finded != null)
                {
                    finded.DebugSelf();
                }
                else
                {
                    Debug.LogError("不在资源引用表中 :" + path);
                }
            }
        }
    }

    //[MenuItem("Window/RefrenceChecker/Clear")]
    private void ClearBar()
    {
        EditorUtility.ClearProgressBar();
    }

    public static void BuildThread(object o)
    {
        string path = (string)o;
        _Map = new RefrenceMap();
        builder.BuildRefrenceMap(_Map, UpdateProgress);        
    }

    public static void UpdateProgress(JobExcuteProgress p)
    {
        jobExcuteProgress = p;
    }
}
